package com.vertx.future;

import com.vertx.util.Runner;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

public class LongTimeVerticle extends AbstractVerticle {
	public static void main(String[] args) {

		Runner.runExample(LongTimeVerticle.class);

	}

	@Override
	public void start(Promise<Void> startPromise) throws Exception {

		HttpServer server = vertx.createHttpServer();

		Router router = Router.router(vertx);

		router.route().handler(BodyHandler.create());

		router.route("/sleep1/:name/:age").handler(req -> {

			String name = req.request().getParam("name");
			System.out.println("begin sleep1");
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			System.out.println("sleep1 10s");
			req.response()
					.putHeader("content-type", "application/json")
					.end(new JsonObject().put("name", name).put("type", "/sleep1/:name/:age").toString());
		});


		server.requestHandler(router).listen(8080);

	}
}
